// +----------------------------------------------------------------------
// | FileName:   ${file_name}  
// +----------------------------------------------------------------------
// | CreateTime: 15/8/21  @上午11:27
// +----------------------------------------------------------------------
// | Author:     xab(admin@xueyong.net.cn)
// +----------------------------------------------------------------------
// | Description:
// +----------------------------------------------------------------------
package cn.xabad.commons.tools;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * Create By Xueyong
 */
public class CipherU {
    private static Cipher encrypt ;
    private static Cipher decrypt ;


    public static Cipher getEncrypt() throws Exception {
        return getEncrypt(MD5.GetMD5Code("xabad").getBytes());
    }
    public static Cipher getEncrypt(byte[] bytes) throws Exception {
        if(encrypt == null){
            byte[] raw = MD5.GetMD5Code16(bytes).getBytes("ASCII");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec iv = new IvParameterSpec(
                    "0102030405060708".getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
            encrypt = cipher ;
        }
        return encrypt;
    }

    public static Cipher getDecrypt() throws Exception {
        return getDecrypt(MD5.GetMD5Code("xabad").getBytes());
    }
    public static Cipher getDecrypt(byte[] bytes) throws Exception {
        if(decrypt ==null){
            byte[] raw = MD5.GetMD5Code16(bytes).getBytes("ASCII");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec iv = new IvParameterSpec(
                    "0102030405060708".getBytes());
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            decrypt = cipher ;
        }
        return decrypt;
    }
}
